package cn.com.dstz.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.type.Type;

import cn.com.dstz.agent.config.AccountConfig;
import cn.com.dstz.base.action.BaseAction;
import cn.com.dstz.security.encoder.Md5PwdEncoder;
import cn.com.dstz.utils.ActionUtils;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;


/** 
 * 交易账号申请栏目管理
 * @author 叶装装
 *
 */
public class AccountAct extends BaseAction{
	 
	/**
	 * 模拟账户
	 */
	public void list_0( ){
		list( 0 );
		return;
	}
	
	/**
	 * 实盘账户
	 */
	public void list_1( ){
		list( 1 );
		return;
	}
	
	public String topass( ){
		
		int validPass = validPass( 2 );
		if( validPass > 0 ){
			alertMsg( null );
			return "pass";
		}
		
		Map< String, Object > account = baseService.getData( "select s.*, m.username from dstzsqzh s, dstzmember m where s.memberid = m.id and s.id=" + id );
		
		if( account == null || account.isEmpty( ) ){
			alertMsg( null );
			return "pass";
		}
		
		getRequest( ).setAttribute( "data", account );
		
		return "pass";
	}
	
	public void pass( ){
		int validPass = validPass( 0 );
		if( validPass > 0 ){
			this.ajaxResponse( validPass );
			return;
		}
		
		// 更新用户表
		int update = baseService.update( "dstzmember", DataBaseUtils.getData( new String[]{
				sptype == 0 ? "mnumber" : "onumber",
				sptype == 0 ? "mpassword" : "opassword"
		}, new Object[]{
				username, password
		} ), DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				memberid
		} ) );
		
		if( update > 0 ){
			// 更新当前表的状态
			update = baseService.update( "dstzsqzh", DataBaseUtils.getData( new String[]{
						"status"
					}, new Object[]{
									1
					} ), DataBaseUtils.getData( new String[]{
						"id"
				}, new Object[]{
						id
				} ) );
		}
		
		this.addLog( update, "account", 0, "ID：" + id, "【账号审核】->" );
		
		this.ajaxResponse( update > 0 ? 0 : -1 );
		
		return;
		
	}
	
	public void copy( ){
		
		int validPass = validPass( 2 );
		if( validPass > 0 ){
			this.ajaxResponse( validPass );
			return;
		}
		
		// 进行copy 
		Map< String, Object > account = baseService.getData( "select s.*, m.username from dstzsqzh s, dstzmember m where s.memberid = m.id and s.id=" + id );
		if( account == null || account.isEmpty( ) ){
			this.ajaxResponse( 1 );
			return;
		}
		
		Map< String, Object > copyData = getCopyData( account );
		Object memberid = account.get( "memberid" );
		
		int copy = baseService.update( "dstzmember", copyData, DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				memberid
		} ) );
		
		this.addLog( copy, "account", 0, "AccountID：" + id + "，MemberID：" + memberid, "【复制信息】->" );
		
		this.ajaxResponse( copy > 0 ? 0 : -1 );
		return;
	}
	
	public void delete( ){
		String ids = getParameter( "ids" );
		
		if( ids != null ){
			String [] splitIds = StringUtils.split( ids, "," );
			
			if( splitIds != null && splitIds.length > 0 ){
				List< String > updateSqlList = new ArrayList< String >( );
				
				Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
				int level = ActionUtils.getAdminLevel( admin );
				
				for( String id : splitIds ){
					if( StringUtils.isNotBlank( id )){
						if( level == 0 ){
							updateSqlList.add( "delete from dstzsqzh where id =" + id);
						}else{
							updateSqlList.add( "update dstzsqzh set status = " + cn.com.dstz.admin.constants.Constants.ACCOUNT_STATUS_DISABLED + " where id =" + id + " and adminid=" + admin.get( "id" ));
						}
					}
				}
				
				int del = baseService.saveOrUpdateBatch( updateSqlList );
				
				this.addLog( del, "account", 2, ids );
				
			}
		}
		
	}
	
	private Map< String, Object > getCopyData( Map< String, Object > data ){
		Map< String, Object > copyData = new HashMap< String, Object >( );
		
		copyData.put( "email", data.get( "txtemail" ) );
		copyData.put( "realname", data.get( "txtname" ) );
		copyData.put( "idcard", data.get( "txtshenfenzheng" ) );
		copyData.put( "telephone", data.get( "txtphone" ) );
		copyData.put( "qqnum", data.get( "txtqq" ) );
		
		return copyData;
	}
	
	
	private int validPass( int pass ){
		if( pass == 2 ){
			if( id == null || id == 0 ){
				return 1;
			}
			return 0;
		}
		
		if( ValidUtils.validLength( username, 3, 20 )){
			return 1;
		}
		
		if( StringUtils.isBlank( password )){
			return 2;
		}
		
		int count = dbValid( );
		
		if( count > 0 ){
			return 3;
		}
		
		if( memberid == null || memberid == 0 ){
			return 4;
		}
		
		return 0;
	}
	
	private int dbValid( ){
		String column = "mnumber";
		if( sptype == 1 ){
			column = "onumber";
		}
		return baseService.countBySQL( "select count(*) as a from dstzmember where " + column + "=:name", DataBaseUtils.getData( new String[]{
				"name"
		}, new Object[]{
				username
		} ) );
	}
	
	private void list( int type ){
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin );
		
		String sql = "select s.id as id, s.aid as aid,  s.txtname as txtname,s.txtshenfenzheng as txtshenfenzheng, s.txtphone as txtphone, s.txtqq as txtqq, s.txtemail as txtemail, s.txtaddress " +
				"as txtaddress, s.sptype as sptype, s.status as status, s.memberid as memberid,m.username as username, a.name as agentname from dstzsqzh s, dstzmember m, dstzagent a where s.memberid = m.id and s.aid = a.id and s.sptype=:sptype ";
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		
		if( level > 0 ){
			
			AccountConfig config = new AccountConfig( admin, this.countManagerGroup( ) );
			sql += config.getSQL( );
			valueMap = config.getValueMap( );
			
			sql += " and s.status!=:status ";
			valueMap.put( "status", cn.com.dstz.admin.constants.Constants.ACCOUNT_STATUS_DISABLED );
			
		}
		valueMap.put( "sptype", type );
	
		
		Map< String, Type > aliasMap = DataBaseUtils.getAlias( new String[]{
				"id", "aid", "txtname", "txtshenfenzheng", "txtphone", "txtqq", "txtemail", "txtaddress", "sptype", "status", "memberid", "username", "agentname"
		}, new Type[]{
				Hibernate.INTEGER, Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING,Hibernate.STRING,
				Hibernate.STRING, Hibernate.STRING, Hibernate.STRING, 
				
				Hibernate.INTEGER, Hibernate.BYTE, Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING
		} );
		
		this.responseToJsonObject( this.paginationBySQL( sql, null, valueMap,  null,  aliasMap )  );
		
	}
	
	private Md5PwdEncoder pwdEncoder;
	
	private Integer id;
	private Integer sptype;
	private Integer memberid;
	
	private String password;
	
	private String username;
	
	public String getPassword( ) {
		return password;
	}

	public void setPassword( String password ) {
		
		if( StringUtils.isBlank( password )){
			password = "dstz.com.cn"; // 这是默认的密码
		}
	
		this.password = pwdEncoder.md5( password );
	}

	public String getUsername( ) {
		return username;
	}

	public void setUsername( String username ) {
		if( StringUtils.isNotBlank( username )){
			username = decode( username );
		}
		this.username = username;
	}

	public Md5PwdEncoder getPwdEncoder( ) {
		return pwdEncoder;
	}

	public void setPwdEncoder( Md5PwdEncoder pwdEncoder ) {
		this.pwdEncoder = pwdEncoder;
	}

	public Integer getId( ) {
		return id;
	}

	public void setId( Integer id ) {
		this.id = id;
	}

	public Integer getSptype( ) {
		if( sptype == null ){
			sptype = 0;
		}
		return sptype;
	}

	public Integer getMemberid( ) {
		return memberid;
	}

	public void setMemberid( Integer memberid ) {
		this.memberid = memberid;
	}

	public void setSptype( Integer sptype ) {
		this.sptype = sptype;
	}

	private static final long serialVersionUID = 1L;
	
	
}
